define(['jquery', 'bootstrap', 'backend', 'table', 'form','selectpage'], function ($, undefined, Backend, Table, Form,selectPage) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'job/index' + location.search,
                    add_url: 'job/add',
                    edit_url: 'job/edit',
                    del_url: 'job/del',
                    multi_url: 'job/multi',
                    import_url: 'job/import',
                    table: 'job',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='city_id']", form).addClass("selectpage").data("source", "area/index").data('params',{"custom[level]":2});
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                showSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'hotel.hotel_name', title: __('Hotel.hotel_name'), operate: 'LIKE'},
                        {field: 'area.name', title: __('城市'), operate: false},
                        {field: 'city_id', title: __('城市'), operate: '',visible: false},
                        {field: 'job_type', title: __('Job_type'), searchList: {"1":__('Job_type 1'),"2":__('Job_type 2'),"3":__('Job_type 3'),"4":__('Job_type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'job_date', title: __('Job_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'job_name', title: __('Job_name'), operate: 'LIKE'},
                        {field: 'job_price', title: __('Job_price'), operate: false},
                        {field: 'company.company_name', title: __('Company.company_name'), operate: 'LIKE'},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        // {field: 'hotel_id', title: __('Hotel_id')},
                        // {field: 'job_company_id', title: __('Job_company_id')},
                        // {field: 'job_is_group', title: __('Job_is_group'), searchList: {"0":__('Job_is_group 0'),"1":__('Job_is_group 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'job_user_group_id', title: __('Job_user_group_id')},
                        // {field: 'job_top_start', title: __('Job_top_start')},
                        // {field: 'job_top_end', title: __('Job_top_end')},
                        // {field: 'job_down_start', title: __('Job_down_start')},
                        // {field: 'job_down_end', title: __('Job_down_end')},
                        // {field: 'job_time_hour', title: __('Job_time_hour')},
                        // {field: 'job_people', title: __('Job_people')},
                        // {field: 'job_speed', title: __('Job_speed')},
                        // {field: 'job_arrival_datetime', title: __('Job_arrival_datetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'job_location', title: __('Job_location'), operate: 'LIKE'},
                        // {field: 'job_route', title: __('Job_route'), operate: 'LIKE'},
                        // {field: 'usergroup.name', title: __('Usergroup.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'view',
                                    text: __('查看'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    refresh: true,
                                    icon: 'fa fa-eye',
                                    url: 'job/view',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'release',
                                    text: __('发布'),
                                    title: __('发布'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    refresh: true,
                                    icon: 'fa fa-check',
                                    visible: function (row, o) {
                                        if (row.status === 1 || row.status === 5) return true
                                    },
                                    url: 'job/release',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'offline',
                                    text: __('下线'),
                                    title: __('下线'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    refresh: true,
                                    icon: 'fa fa-arrow-down',
                                    visible: function (row, o) {
                                        if (row.status === 3) return false
                                        if (row.status > 1 && row.status !== 5 && row.status !== 4 ) return true
                                    },
                                    url: 'job/offline',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'edit',
                                    title: '编辑',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    refresh: true,
                                    icon: 'fa fa-pencil',
                                    visible: function (row, o) {
                                        if (row.status === 5) return true
                                    },
                                    url: 'job/edit',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 批量操作 发布/下线
            $(".btn-more").on("click", function () {
                let act = $(this).data('act');
                let data = table.bootstrapTable('getSelections');
                for (let i of data){
                    if(i.status === 1 && act === 5 ){
                        Toastr.error('未发布的招聘信息不可下线');
                        return false
                    }
                    if(i.status === 3 && act === 5 ){
                        Toastr.error('进行中的招聘信息不可下线');
                        return false
                    }
                    if(i.status === 3 && act === 2 ){
                        Toastr.error('进行中的招聘信息不可操作');
                        return false
                    }
                }
                let ids = data.map(x => {return x.id}).join(',')
                $.post({
                    url: 'job/batch?act='+act+'&ids=' + ids,
                    success: function (res) {
                        if (res.code === 0) {
                            Toastr.error(res.msg);
                        } else {
                            Toastr.success(res.msg);
                        }
                        $(".btn-refresh").trigger("click");
                    }
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            var job_top_start = $("#job_top_start");
            var job_top_end = $("#job_top_end");
            var job_down_start = $("#job_down_start");
            var job_down_end = $("#job_down_end");
            var job_user_group_id = $("#job_user_group_id");
            var job_time_hour = $("#c-job_time_hour");

            var form = $('form[role=form]');

            // 选择酒店，请求简介
            $('#c-hotel_id').selectPage({
                keyField: 'id',
                showField: 'hotel_name',
                selectOnly: false,
                pageSize: 10,
                data: 'hotel/index',
                eAjaxSuccess: function (data) {
                    data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
                    data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
                    return data;
                },
                eSelect: function (data) {
                    $.get({
                        url: 'hotel/getContent?ids=' + data.id,
                        success: function (data) {
                            $("#c-job_hotel_introduce").val(data.data)
                            UE.list['c-job_hotel_introduce'].setContent(data.data);
                        }
                    })
                },
            });

            // 自定义班验证
            $('#c-job_type').on('change', function () {
                var val = Number($(this).val());
                if (val === 4 || val === 3) {
                    form.validator("setField", 'row[job_top_start]', "required,range(0~12, false)");
                    form.validator("setField", 'row[job_top_end]', "required,range(0~12, false)");
                    form.validator("setField", 'row[job_down_start]', "required,range(12~24, false)");
                    form.validator("setField", 'row[job_down_end]', "required,range(12~24, false)");
                    job_top_start.show();
                    job_top_end.show();
                    job_down_start.show();
                    job_down_end.show();
                } else {
                    form.validator("setField", 'row[job_top_start]', null);
                    form.validator("setField", 'row[job_top_end]', null);
                    form.validator("setField", 'row[job_down_start]', null);
                    form.validator("setField", 'row[job_down_end]', null);
                    job_top_start.hide();
                    job_top_end.hide();
                    job_down_start.hide();
                    job_down_end.hide();
                    if(val === 1) job_time_hour.val(4);
                    if(val === 2) job_time_hour.val(8);
                }
            });

            // 分组发布验证
            $('#c-job_is_group').on('change', function () {
                var val = Number($(this).val());
                if (val === 1) {
                    form.validator("setField", 'row[job_user_group_id]', "required,range(0~),integer(+)");
                    job_user_group_id.show();
                } else {
                    form.validator("setField", 'row[job_user_group_id]', null);
                    job_user_group_id.hide();
                }
            });

            Controller.api.bindevent();
        },
        edit: function () {
            var job_top_start = $("#job_top_start");
            var job_top_end = $("#job_top_end");
            var job_down_start = $("#job_down_start");
            var job_down_end = $("#job_down_end");
            var job_user_group_id = $("#job_user_group_id");
            var job_time_hour = $("#c-job_time_hour");

            var form = $('form[role=form]');

            // 选择酒店，请求简介
            $('#c-hotel_id').selectPage({
                keyField: 'id',
                showField: 'hotel_name',
                selectOnly: false,
                pageSize: 10,
                data: 'hotel/index',
                eAjaxSuccess: function (data) {
                    data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
                    data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
                    return data;
                },
                eSelect: function (data) {
                    $.get({
                        url: 'hotel/getContent?ids=' + data.id,
                        success: function (data) {
                            $("#c-job_hotel_introduce").val(data.data)
                            UE.list['c-job_hotel_introduce'].setContent(data.data);
                        }
                    })
                },
            });

            // 自定义班验证
            $('#c-job_type').on('change', function () {
                var val = Number($(this).val());
                if (val === 4 || val ===3) {
                    form.validator("setField", 'row[job_top_start]', "required,range(0~12, false)");
                    form.validator("setField", 'row[job_top_end]', "required,range(0~12, false)");
                    form.validator("setField", 'row[job_down_start]', "required,range(12~24, false)");
                    form.validator("setField", 'row[job_down_end]', "required,range(12~24, false)");
                    job_top_start.show();
                    job_top_end.show();
                    job_down_start.show();
                    job_down_end.show();
                } else {
                    form.validator("setField", 'row[job_top_start]', null);
                    form.validator("setField", 'row[job_top_end]', null);
                    form.validator("setField", 'row[job_down_start]', null);
                    form.validator("setField", 'row[job_down_end]', null);
                    job_top_start.hide();
                    job_top_end.hide();
                    job_down_start.hide();
                    job_down_end.hide();
                    if(val === 1) job_time_hour.val(4);
                    if(val === 2) job_time_hour.val(8);
                }
            });

            // 分组发布验证
            $('#c-job_is_group').on('change', function () {
                var val = Number($(this).val());
                if (val === 1) {
                    form.validator("setField", 'row[job_user_group_id]', "required,range(0~),integer(+)");
                    job_user_group_id.show();
                } else {
                    form.validator("setField", 'row[job_user_group_id]', null);
                    job_user_group_id.hide();
                }
            });


            Controller.api.bindevent();
        },
        view: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});